#!/usr/bin/python

__author__  = "author"
__mail__    = "mail"
__version__ = (0,0,1)

from Blogger import Blogger
from datetime import datetime

"""
  Insert program explanation here
"""
##############################################################################
"""
  This object is the container for a blog entry.
"""
class BloggerTarget(object):

      def __init__(self):
          pass

      def add_feed_title(self, value):
          print 'feed title: %s' % value

      def add_entry_title(self, value):
          print 'entry title: %s' % value

      def add_entry_content(self, value):
          print 'entry content'
          print '     %s' % value

##############################################################################
class BloggerInterface(object):

      def __init__(self, configuration):
          
          if configuration.has_key('blog_service'):
              if configuration['blog_service'] is not 'blogger':
                 raise ValueError('bad blog_service: %' % 
                          configuration['blog_service'])
          if configuration.has_key('blog_user'):
              self.user = configuration['blog_user']
          else:
              raise ValueError('no blog_user')
          if configuration.has_key('blog_password'):
              self.password = configuration['blog_password']
          else:
              raise ValueError('no blog_password')
          if configuration.has_key('blog_source'):
              self.source = configuration['blog_source']
          else:
              raise ValueError('no blog_source')
          self.blog = Blogger(self.user, self.password, self.source)

      def start(self):
          pass

      def write(self, title, content, author_name):
          try:
             self.blog.CreatePost(title, content, author_name, is_draft=False)
          except Exception, ex:
             print "BloggerInterface.write blues: %s" % repr(ex) 
             return False
          return True

      def download(self, target):
          try:
             self.blog.GetPosts(target)
          except Exception, ex:
             print "BloggerInterface.download blues: %s" % repr(ex) 
             return False
          return True

      def is_ready(self):

          try:
             now = datetime.now()
             message = '%d - %d - %d' % (now.year, now.month, now.day)
             self.start()
             if self.write('Ready for working', message, 'infobot') is False:
                raise ValueError('check_writable_blog: blog not writable.')
             self.stop()
          except Exception, ex:
             print "BloggerInterface.is_ready blues: %s" % repr(ex) 
             return False
          return True

      def stop(self):
          pass


